package com.hyg.lib_common.loginpart;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hyg.lib_base.mainUtils.StatusBarUtil;
import com.hyg.lib_base.mainUtils.Utils;
import com.hyg.lib_common.Base.BaseActivity;
import com.hyg.lib_common.DataModel.EventBusMessage;
import com.hyg.lib_common.DataModel.ReceiveData;
import com.hyg.lib_common.Net.CommonHttpUtil;
import com.hyg.lib_common.constant.Constants;
import com.hyg.lib_common.constant.UrlUtils;
import com.hyg.lib_common.R;
import com.hyg.lib_common.WebView.WebViewActivity;
import com.hyg.lib_common.databinding.ActivityLoginByCodeBinding;
import com.hyg.lib_common.sweetalertdialog.SweetAlertDialog;

import org.greenrobot.eventbus.Subscribe;
import org.json.JSONException;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.ref.WeakReference;
import java.net.URLDecoder;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.Response;

public class LoginByCodeActivity extends BaseActivity {
    ActivityLoginByCodeBinding binding;
    public String mphone = "[1][3456789]\\d{9}";//第一位是数字1，第二位是358中的一个在加上9个数字
    String type = "login";
    String openid="";

    //    private MyHandler myHandler = new MyHandler(this);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityLoginByCodeBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        init();
    }

    public void init() {
        if(!TextUtils.isEmpty(getIntent().getStringExtra("type"))){
            type = getIntent().getStringExtra("type");
        }
        if(!TextUtils.isEmpty(getIntent().getStringExtra("openid"))){
            openid = getIntent().getStringExtra("openid");
        }
        if(type.equals("login")){
            binding.lnTitleView.tvTitle.setVisibility(View.GONE);
        }else{
            binding.lnTitleView.tvTitle.setText("绑定手机号码");
            binding.lnTitleView.tvTitle.setVisibility(View.VISIBLE);
        }
        initView();
    }

    public void initView() {
        StatusBarUtil.setStatusBar(1, this, getResources().getColor(R.color.white), 00, null, null, true);
        binding.lnTitleView.back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        binding.btnGetcode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendCode();
            }
        });
        binding.tvAgreement.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent it = new Intent(LoginByCodeActivity.this, WebViewActivity.class);
                it.putExtra("url", UrlUtils.PrivacyUrl);
                it.putExtra("title", "隐私政策与免责声明");
                it.putExtra("type", "Privacy");
                startActivity(it);
            }
        });

    }


    public void sendCode() {
        String phone = binding.etPhone.getText().toString();
        if (Utils.isFastClick()) {
            return;
        }
        if (phone.equals("")) {
            if (pDialog.isShowing())
                pDialog.dismiss();
            pDialog = new SweetAlertDialog(LoginByCodeActivity.this, SweetAlertDialog.ERROR_TYPE);
            pDialog.setTitleText("格式错误");
            pDialog.setContentText("电话号码不可为空，请填写电话号码");
            pDialog.setConfirmButton("好，知道啦", new SweetAlertDialog.OnSweetClickListener() {
                public void onClick(SweetAlertDialog sweetAlertDialog) {
                    sweetAlertDialog.dismiss();
                }
            });
            pDialog.show();
        } else if (!phone.matches(mphone)) {
            if (pDialog.isShowing())
                pDialog.dismiss();
            pDialog = new SweetAlertDialog(LoginByCodeActivity.this, SweetAlertDialog.ERROR_TYPE);
            pDialog.setTitleText("格式错误");
            pDialog.setContentText("电话号码格式不对，请重新输入。");
            pDialog.setConfirmButton("好，知道啦", new SweetAlertDialog.OnSweetClickListener() {
                public void onClick(SweetAlertDialog sweetAlertDialog) {
                    sweetAlertDialog.dismiss();
                }
            });
            pDialog.show();
            binding.etPhone.setText("");
        } else {
            CommonHttpUtil.getCode(phone, new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    ErrorDialog("连接服务器失败，请稍后重试！");
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    String result = response.body().string();
                    Log.i("lgb", "onResponse: "+result);
                    ReceiveData receiveData = JSONObject.parseObject(result, ReceiveData.class);
                    if (receiveData.getCode().equals("200")) {
                        Intent intent = new Intent(LoginByCodeActivity.this, VerificationLoginActivity.class);
                        intent.putExtra("phone", phone);
                        intent.putExtra("type", type);
                        intent.putExtra("openid", openid);
                        startActivity(intent);
                    } else {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                ErrorDialog(receiveData.getMessage());
                            }
                        });
                    }
                }
            });
        }
    }


    @Subscribe
    public void onEvent(EventBusMessage message) {
        //接收以及处理数据
        if (message.getmessage().equals("startMainActivity")) {
            finish();
        }
    }


}